<?php
class TestimonialsController extends AppController {

	var $name = 'Testimonials';
	var $helpers = array('Html', 'Form');

    
    function index() 
    {
        $this->set('choose1',6); 
        //$this->Testimonial->recursive = 0;
        //$testimonials = $this->Testimonial->find('all',array('conditions'=>array('Testimonial.status'=>1),'order'=>'Testimonial.created'));
        //$testimonials = $this->Testimonial->query("SELECT testimonials.*,jobseekers.*,employers.* FROM `testimonials`");
        
        $jobseeker_sql = "  SELECT testimonials.*,jobseekers.* 
                            FROM `testimonials` LEFT JOIN `jobseekers` ON ( jobseekers.id = testimonials.jobseeker_id )
                            WHERE testimonials.status=1 AND testimonials.from='jobseeker' ORDER BY testimonials.created DESC";
        $jobseeker_testimonials = $this->Testimonial->query($jobseeker_sql); 
        $this->set('jobseeker_testimonials', $jobseeker_testimonials);
    
        $employer_sql = "  SELECT testimonials.*,employers.* 
                            FROM `testimonials` LEFT JOIN `employers` ON ( employers.id = testimonials.employer_id )
                            WHERE testimonials.status=1 AND testimonials.from='employer' ORDER BY testimonials.created DESC";
        $employer_testimonials = $this->Testimonial->query($employer_sql); 
        $this->set('employer_testimonials', $employer_testimonials);
    
    
    }    
    
    function post()
    {
        if($this->Session->read('User'))
        {
            if(!empty($this->data))
            {
                $this->Testimonial->recursive=-1;                
                if($this->Session->read('User.type')=='jobseeker')
                {
                    $this->data['Testimonial']['from']='jobseeker';
                    $this->data['Testimonial']['jobseeker_id']=$this->Session->read('User.id');
                    $data = $this->Testimonial->find('count',array('conditions'=>array('Testimonial.from'=>'jobseeker','Testimonial.jobseeker_id'=>$this->Session->read('User.id'))));
                }

                if($this->Session->read('User.type')=='employer')
                {
                    $this->data['Testimonial']['from']='employer';
                    $this->data['Testimonial']['employer_id']=$this->Session->read('User.id');
                    $data = $this->Testimonial->find('count',array('conditions'=>array('Testimonial.from'=>'employer','Testimonial.employer_id'=>$this->Session->read('User.id'))));
                }

                
                if($data)
                {
                    $this->Session->setFlash(__('Sorry, Each member only can post a quote.', true));
                    $this->redirect(array('controller'=>'testimonials', 'action'=>'index'),null, true);
                }
                else
                {
                    $this->Testimonial->create();
                    $this->Testimonial->save($this->data);
                    $this->Session->setFlash(__('Thank you for your testimonial.', true));
                    $this->redirect(array('controller'=>'testimonials', 'action'=>'index'),null, true);
                }
            }
        }
        else
        {  
            $this->Session->setFlash(__('Please Login first', true));
            $this->redirect(array('controller'=>'testimonials', 'action'=>'index'),null, true);
        }
    }

    function index_admin()  
    {
        if($this->Session->read('User.type')=='admin')
        {
            $this->layout='ajax';
        }
        else
        {
            $this->Session->setFlash('You are forbidden for this page');
            $this->redirect(array('controller'=>'jobs', 'action'=>'login_admin'),null, true);
        }             
    }
    
    function filter_admin()  
    {
        $this->layout='ajax';
        if(!empty($this->data['category']))
        {
            $this->Testimonial->recursive=0;
                                                                 
            if($this->data['category']==1)
            { 
                $query = "SELECT testimonials.*, employers.company_name , employers.salution, employers.full_name FROM `testimonials`,`employers` WHERE testimonials.employer_id = employers.id AND testimonials.from='employer'";
            }
            if($this->data['category']==2)
            { 
                $query = "SELECT testimonials.*, jobseekers.salution, jobseekers.first_name, jobseekers.last_name FROM `testimonials`,`jobseekers` WHERE testimonials.jobseeker_id = jobseekers.id AND testimonials.from='jobseeker'"; 
            }

            $mydate = date("Y-m-d", mktime(0, 0, 0, date('m')-1, date('d'), date('Y')));
            if($this->data['status']==2)
            { $query.= " AND testimonials.status = 1"; }
            if($this->data['status']==3)
            { $query.= " AND testimonials.status = 1 AND testimonials.created < '".$mydate."'"; }
            if($this->data['status']==4)
            { $query.= " AND testimonials.status = 0"; }
            if($this->data['status']==5)
            { $query.= " AND testimonials.status = 0 AND testimonials.created < '".$mydate."'"; }
            
            $query.= " ORDER BY testimonials.id ";
            $testimonials = $this->Testimonial->query($query);
            $this->set('testimonials',$testimonials);
        }
    }
    
    function edit_admin( $id = null )
    {
        if(!empty($this->data))
        {
            $this->Testimonial->save($this->data);
            $this->redirect(array('controller'=>'testimonials','action'=>'index_admin'));    
        }
        else
        {
            $this->layout = 'ajax';
            $this->Testimonial->recursive=-1;
            $this->data = $this->Testimonial->read(null,$id);
        }
    }    

    function approve_admin( $id = null )
    { 
        $this->data['Testimonial']['id']=$id;
        $this->data['Testimonial']['status']=1;
        $this->data['Testimonial']['created']=date("Y-m-d");
        $this->Testimonial->save($this->data);
        $this->redirect(array('controller'=>'testimonials','action'=>'index_admin'));
    }

    function cancel_admin( $id = null )
    { 
        $this->data['Testimonial']['id']=$id;
        $this->data['Testimonial']['status']=0;
        $this->Testimonial->save($this->data);
        $this->redirect(array('controller'=>'testimonials','action'=>'index_admin'));
    }
    
    function delete_admin($id)
    {  
        if($this->Session->read('User.type')=='admin')
        {
            $this->layout = 'ajax';
            $this->Testimonial->del($id);
            $this->redirect(array('controller'=>'testimonials','action'=>'index_admin'));
        }        
    }    
    

}
?>